package com.platform.modules.app.controller;

import com.alibaba.fastjson.JSON;
import com.platform.annotation.LoginUser;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.Constant;
import com.platform.common.utils.JedisUtil;
import com.platform.common.utils.RestResponse;
import com.platform.modules.mall.entity.MallAccessRecordsGoodsEntity;
import com.platform.modules.mall.entity.MallAccessRecordsPageEntity;
import com.platform.modules.mall.entity.MallUserEntity;
import com.platform.modules.mall.service.MallAccessRecordsGoodsService;
import com.platform.modules.mall.service.MallAccessRecordsPageService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.SimpleFormatter;

@Api(tags = "AppAccessRecordsController|访问记录")
@RestController
@RequestMapping("/app/accessRecords")
public class AppAccessRecordsController extends AppBaseController {

    @Resource
    private JedisUtil jedisUtil;

    @GetMapping("/access")
    @ApiOperation(value = "访问记录", notes = "访问记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "int", name = "type", value = "访问类型，0：页面访问，1：商品访问", example = "0", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "string", name = "goodsId", value = "goodsId", example = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "string", name = "pagePath", value = "pagePath", example = "", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "string", name = "pageName", value = "pageName", example = "", required = false, dataType = "string"),
    })
    public RestResponse access(@LoginUser MallUserEntity user, @RequestParam Map<String, Object> jsonParam) {
        String type = jsonParam.get("type").toString();
        String ip = this.getClientIp();
        String goodsId = jsonParam.get("goodsId") == null ? null : jsonParam.get("goodsId").toString();
        String pagePath = jsonParam.get("pagePath") == null ? null : jsonParam.get("pagePath").toString();
        String pageName = jsonParam.get("pageName") == null ? null : jsonParam.get("pageName").toString();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String limitRedisKey;
        String dataRedisKey;
        String jsonStr;
        //页面访问记录
        if ("0".equals(type)) {

            limitRedisKey = Constant.ACCESS_RECORDS_LIMIT + user.getId() + ip + pagePath + pageName;
            dataRedisKey = Constant.ACCESS_RECORDS_PAGE + dateStr;

            MallAccessRecordsPageEntity pageEntity = new MallAccessRecordsPageEntity();
            pageEntity.setUserId(user.getId());
            pageEntity.setIp(ip);
            pageEntity.setPagePath(pagePath);
            pageEntity.setPageName(pageName);
            pageEntity.setAddTime(new Date());

            //判断十秒内是否访问过
            if (!jedisUtil.exists(limitRedisKey)) {
                jedisUtil.set(limitRedisKey, "", 10);

                //缓存访问记录数据
                List<MallAccessRecordsPageEntity> list;
                if (jedisUtil.exists(dataRedisKey)) {
                    jsonStr = jedisUtil.get(dataRedisKey);
                    list = JSON.parseArray(jsonStr, MallAccessRecordsPageEntity.class);
                } else {
                    list = new ArrayList<>();
                }
                list.add(pageEntity);
                jedisUtil.set(dataRedisKey, JSON.toJSONString(list), 129600);

            }

            //商品访问记录
        } else if ("1".equals(type)) {
            limitRedisKey = Constant.ACCESS_RECORDS_LIMIT + user.getId() + ip + goodsId;
            dataRedisKey = Constant.ACCESS_RECORDS_GOODS + dateStr;

            MallAccessRecordsGoodsEntity goodsEntity = new MallAccessRecordsGoodsEntity();
            goodsEntity.setUserId(user.getId());
            goodsEntity.setIp(ip);
            goodsEntity.setGoodsId(goodsId);
            goodsEntity.setAddTime(new Date());


            //判断十秒内是否访问过
            if (!jedisUtil.exists(limitRedisKey)) {
                jedisUtil.set(limitRedisKey, "", 10);

                //缓存访问记录数据
                List<MallAccessRecordsGoodsEntity> list;
                if (jedisUtil.exists(dataRedisKey)) {
                    jsonStr = jedisUtil.get(dataRedisKey);
                    list = JSON.parseArray(jsonStr, MallAccessRecordsGoodsEntity.class);
                } else {
                    list = new ArrayList<>();
                }
                list.add(goodsEntity);
                jedisUtil.set(dataRedisKey, JSON.toJSONString(list), 129600);

            }
        } else {
            throw new BusinessException("访问类型有误！");
        }


        return RestResponse.success();
    }

}
